package com.sportradar.assessment.work.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.ResourceBundle;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sportradar.assessment.work.beans.ConfigurationBean;

public class ConfigUtil {

	private static Map<String, String> configProperties=new HashMap<String, String>();
	private static Logger logger=LoggerFactory.getLogger(ConfigUtil.class);
	
	
	
	public static ConfigurationBean update(){
		
		configProperties.clear();
		File propertyFile = new File(Constants.DEFAULT_CONFIG_FILE_PATH);
		if (propertyFile.exists()) {
			configurePropertiesFromFile();
		} else {
			logger.warn("Configuration file "+Constants.DEFAULT_CONFIG_FILE_PATH+" not found. Settings loaded from default bundle");
			configurePropertiesFromBundle();
		}

		String activeThreadName = configProperties.get(Constants.ACTIVE_THREAD_NAME_KEY);
		String keyCurrentInterval = activeThreadName + ".execution.interval";
		long currentExecutionInterval = getPropertyValueAsLong(keyCurrentInterval);
		long updatesSettingsInterval = getPropertyValueAsLong(Constants.REFRESH_INTERVAL_KEY);

		ConfigurationBean configBean = new ConfigurationBean();
		configBean.setIntervalBetweenCalculations(currentExecutionInterval);
		configBean.setIntervalBetweenConfigRefresh(updatesSettingsInterval);
		configBean.setCurrentEnabledThreadName(activeThreadName);
		return configBean;
	}
	
	
	public static String getPropertyValue(String propertyName){
		String propertyFound=configProperties.get(propertyName);
		if(propertyFound==null){
			throw new IllegalArgumentException(propertyName+" cannot be null");
		}
		return propertyFound;
	}
	
	public static long getPropertyValueAsLong(String propertyName){
		String propertyFound=getPropertyValue(propertyName);
		return Long.parseLong(propertyFound);
	}
	

	private static void configurePropertiesFromBundle(){	  
		logger.info("Loading settings from Bundle "+Constants.DEFAULT_CONFIG_BUNDLE );
		
		configProperties = new HashMap<String, String>();
		ResourceBundle bundle = ResourceBundle.getBundle(Constants.DEFAULT_CONFIG_BUNDLE);
		Enumeration<String> keys = bundle.getKeys();
		while (keys.hasMoreElements()) {
			String key = keys.nextElement();
			String value = bundle.getString(key);
			configProperties.put(key, value);
		}
	}
	
	private static void configurePropertiesFromFile(){
		logger.info("Loading settings from Path "+Constants.DEFAULT_CONFIG_FILE_PATH );
		Properties prop = new Properties();
		FileInputStream fileInputStream=null;
    	try {
    		fileInputStream=new FileInputStream(Constants.DEFAULT_CONFIG_FILE_PATH);
         	prop.load(fileInputStream);
         	Enumeration keys =prop.keys();
         	while (keys.hasMoreElements()) {
    			String key = (String)keys.nextElement();
    			String value = prop.getProperty(key);
    			configProperties.put(key, value);
    		}

    	} catch (IOException ex) {
    		logger.error(ex.getMessage(),ex);
        }finally{
        	try {
				fileInputStream.close();
			} catch (IOException ex) {
				logger.error(ex.getMessage(),ex);
			}
        }
	}
	

}
